/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package service.menu;

import javax.annotation.Resource;

import lombok.extern.log4j.Log4j;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import service.https.HttpsService;

import common.Constants;
import common.HttpsUtil;
import common.annotation.ServiceLog;

import entity.menu.Button;
import entity.menu.CommonButton;
import entity.menu.ComplexButton;
import entity.menu.Menu;

/**
 * @author Mht
 * @date May 31, 2016
 */
@Log4j
@Service
public class MenuService {

	@Resource
	private HttpsService httpsService;

	private final static String createMenuUrl = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

	public boolean createMenu(Menu menu, String accessToken) {

		int result = 0;

		String requestUrl = createMenuUrl.replace("ACCESS_TOKEN", accessToken);

		String jsonMenu = JSONObject.fromObject(menu).toString();

		JSONObject object = httpsService.httpRequest(requestUrl, Constants.HTTP_POST, jsonMenu);

		if (null != object) {
			if (0 != object.getInt(Constants.ERROR_CODE)) {
				result = object.getInt(Constants.ERROR_CODE);
				log.error(String.format("创建菜单失败 -- ErrorCode : {%n} ErrorMsg : {%s}",
						object.getInt(Constants.ERROR_CODE), object.getString(Constants.ERROR_MSG)));
			}
		}
		return 0 == result ? true : false;
	}

	public boolean createMenuWithStatic(Menu menu, String accessToken) {

		int result = 0;

		String requestUrl = createMenuUrl.replace("ACCESS_TOKEN", accessToken);

		String jsonMenu = JSONObject.fromObject(menu).toString();

		JSONObject object = HttpsUtil.httpRequest(requestUrl, Constants.HTTP_POST, jsonMenu);

		if (null != object) {
			if (0 != object.getInt(Constants.ERROR_CODE)) {
				result = object.getInt(Constants.ERROR_CODE);
				log.error(String.format("创建菜单失败 -- ErrorCode : {%n} ErrorMsg : {%s}",
						object.getInt(Constants.ERROR_CODE), object.getString(Constants.ERROR_MSG)));
			}
		}
		return 0 == result ? true : false;
	}
	
	public Menu assembleMenu() {
		CommonButton btn11 = new CommonButton();
		btn11.setName("天气预报");
		btn11.setType(Constants.EVENT_TYPE_CLICK);
		btn11.setKey("11");
		CommonButton btn14 = new CommonButton();
		btn11.setName("公交查询");
		btn11.setType(Constants.EVENT_TYPE_CLICK);
		btn11.setKey("14");

		CommonButton btn12 = new CommonButton();
		btn11.setName("电影排行榜");
		btn11.setType(Constants.EVENT_TYPE_CLICK);
		btn11.setKey("12");
		CommonButton btn15 = new CommonButton();
		btn11.setName("经典游戏");
		btn11.setType(Constants.EVENT_TYPE_CLICK);
		btn11.setKey("15");

		CommonButton btn13 = new CommonButton();
		btn11.setName("幽默笑话");
		btn11.setType(Constants.EVENT_TYPE_CLICK);
		btn11.setKey("13");
		CommonButton btn16 = new CommonButton();
		btn11.setName("美女电台");
		btn11.setType(Constants.EVENT_TYPE_CLICK);
		btn11.setKey("16");

		ComplexButton cb1 = new ComplexButton();
		cb1.setName("生活助手");
		cb1.setSubButton(new CommonButton[] { btn11, btn14 });

		ComplexButton cb2 = new ComplexButton();
		cb2.setName("休闲驿站");
		cb2.setSubButton(new CommonButton[] { btn12, btn15 });

		ComplexButton cb3 = new ComplexButton();
		cb3.setName("更多体验");
		cb3.setSubButton(new CommonButton[] { btn13, btn16 });

		Menu menu = new Menu();
		menu.setButton(new Button[] { cb1, cb2, cb3 });

		return menu;
	}
	
	@ServiceLog(description="Test AOP........")
	public void testServiceAop() throws Exception{
		throw new Exception("Test AOP Service!!!");
	}
}
